package com.xyht.sca_s.student_manage_system.modules.assetConsumable.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.req.SmsAssetConsumableStorageReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.service.SmsAssetConsumableStorageService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 易耗品资产库 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-06-27
 */
@RestController
@RequestMapping("/consumableManage")
@SecretBody
public class SmsAssetConsumableStorageController {
    @Autowired
    private SmsAssetConsumableStorageService smsAssetConsumableStorageService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "易耗品单个添加")
    @PostMapping("/add")
    public ResponseResult singleConsumableStorage(@RequestBody @Validated SmsAssetConsumableStorageReq consumableStorageReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetConsumableStorageService.singleAddConsumableStorage(consumableStorageReq, user_id);
    }

    @ApiModelProperty(value = "获取易耗品列表")
    @GetMapping("/list")
    public ResponseResult getConsumableStorage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "searchStr", required = false) String searchStr,
                                               @RequestParam(value = "consumableCategory", required = false) String consumableCategory,
                                               @RequestParam(value = "consumableWarehouse", required = false) String consumableWarehouse) {
        return smsAssetConsumableStorageService.getConsumableStorage(pageNum, pageSize, searchStr, consumableCategory, consumableWarehouse);
    }

    @ApiModelProperty(value = "易耗品批量导入")
    @PostMapping("/batch")
    public ResponseResult batchAddConsumableStorage(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsAssetConsumableStorageService.batchAddConsumableStorage(map, userId);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }
}

